<?php

namespace App\Repository;

use App\Entity\Config;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Common\Persistence\ManagerRegistry;
use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\ODM\MongoDB\Repository\DocumentRepository;

/**
 * @method Config|null find($id, $lockMode = null, $lockVersion = null)
 * @method Config|null findOneBy(array $criteria, array $orderBy = null)
 * @method Config[]    findAll()
 * @method Config[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
 */
class ConfigRepository extends DocumentRepository
{
    public function __construct(DocumentManager $dm)
    {
        parent::__construct($dm, $dm->getUnitOfWork(),$dm->getClassMetadata(Config::class));
    }

    /**
     * @param $k
     * @return string|null
     */
    public function getByK($k)
    {
        $conf = $this->findOneBy(array("k"=>$k));
        if ($conf) {
            return  $conf->getV();
        } else {
            return  "";
        }
    }

    /**
     * 处理成对的数字如1：2,2：5，...这类成对出现的
     * @param $k
     * @return array
     */
    public function getKVNumArray($k)
    {
        $charge = $this->getByK($k);
        $money = myExplodeNumbers($charge);
        $moneySet = [];
        $cound = floor(count($money)/2);
        for ($i= 0;$i<$cound;$i++) {
            $moneySet[]=[$money[$i * 2],$money[$i * 2 + 1]];
        }
        return $moneySet;
    }

}
